package com.deyuanyun.pic.settings.controller.prvlg;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.prvlg.MenuVO;
import com.deyuanyun.pic.settings.domain.prvlg.Menu;
import com.deyuanyun.pic.settings.service.prvlg.MenuService;
import com.deyuanyun.pic.settings.service.prvlg.RoleService;
import com.github.pagehelper.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/settings/prvlg/menu")
public class MenuController {
	@Resource
	private RoleService roleService;
	@Resource
	private MenuService menuService;

	//菜单管理页面
	@RequestMapping(value="/toMenuMgr.html",method=RequestMethod.GET)
	public ModelAndView menuPage(ModelAndView mv) {
		mv.setViewName("/settings/prvlg/menuMgr");
		return mv;
	}

	@RequestMapping(value="/getMenus.json",method=RequestMethod.POST)
	public void getMenus(String menuName) {
		AjaxSupport.sendSuccessText("", menuService.queryMenuListByName(menuName));
	}

	@RequestMapping(value="/queryMenuList.json",method=RequestMethod.POST)
	@ResponseBody
	public void queryMenuList(String menuName) {


		MenuVO menuVO = new MenuVO();

		PageJsonBean jsonBean = new PageJsonBean();
		Page<Menu> list = menuService.queryMenuListByName(menuName);
		List<MenuVO> menuVOList = menuVO.toDomain(list);
		jsonBean.setTotal(((Long)list.getTotal()).intValue());
		jsonBean.setList(menuVOList);
		AjaxSupport.sendSuccessText("", jsonBean);
	}

	@RequestMapping(value="/updateMenuState.json",method=RequestMethod.POST)
	public ModelAndView updateMenuState(HttpServletRequest request){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		ModelAndView mv = new ModelAndView();
		try {
			roleService.updateMenuState(param);
			mv.addObject("status", "0");
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("status", "1");
		}
		return mv;
	}

	@RequestMapping(value="/updateMenu.json",method=RequestMethod.POST)
	public ModelAndView updateMen(HttpServletRequest request){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		ModelAndView mv = new ModelAndView();
		try {
			Boolean status=roleService.updateMenu(param);if(status)
			{
				mv.addObject("status", "0");
			}else{
				mv.addObject("status", "1");
				mv.addAllObjects(param);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("status", "1");
		}
		return mv;
	}

	//=========独立清管
	@RequestMapping(value="/updateMenu1.json",method=RequestMethod.POST)
	@ResponseBody
	public void updateMen1(HttpServletRequest request,
						   String id,
						   String showName,
						   String sort,
						   String createRemark){
		Asserts.assertTrue(StringUtils.isNotBlank(id),"id不能为空");
		HashMap map=new HashMap();
		map.put("MENUNAME",showName);
		map.put("ID_",id);
		map.put("CREATE_REMARK",createRemark);
		map.put("SORT",sort);
		Boolean status=roleService.updateMenu(map);
		Asserts.assertTrue(status,"修改失败");
		AjaxSupport.sendSuccessText("","修改成功");
		return;
	}
}
